#! /bin/sh

# Nexgenta Build Tools
# http://developer.nexgenta.com/buildtools/
#
# Copyright (c) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 Mo McRoberts.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. The names of the author(s) of this software may not be used to endorse
#    or promote products derived from this software without specific prior
#    written permission.
#
# THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, 
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
# AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
# AUTHORS OF THIS SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

if test -r "$HOME/.autogenrc" ; then
	. "$HOME/.autogenrc"
fi

AUTOHEADER26=${AUTOHEADER26-autoheader26}
LIBTOOLIZE15=${LIBTOOLIZE15-libtoolize15}
ACLOCAL110=${ACLOCAL110-aclocal110}
AUTOMAKE110=${AUTOMAKE110-automake110}
AUTOCONF26=${AUTOCONF26-autoconf26}
AUTORECONF=${AUTORECONF-autoreconf}
GPATCH=${GPATCH-patch}

## Set these for autoreconf
AUTOHEADER=${AUTOHEADER-${AUTOHEADER26}}
export AUTOHEADER
LIBTOOLIZE=${LIBTOOLIZE-${LIBTOOLIZE15}}
export LIBTOOLIZE
ACLOCAL=${ACLOCAL-${ACLOCAL110}}
export ACLOCAL
AUTOMAKE=${AUTOMAKE-${AUTOMAKE110}}
export AUTOMAKE
AUTOCONF=${AUTOCONF-${AUTOCONF26}}
export AUTOCONF

appname="$0"
srcdir="`pwd`"

recurse=yes
apply_patches=yes
force_patches=no
quiet=no

autogen_help() {
	echo "usage: $0 [OPTIONS]"
	echo ""
	echo "OPTIONS are one or more of:"
	echo "  --help                   Display this message and exit"
	echo "  --no-recurse or -nr      Don't recurse into subdirectories"
	echo "  --no-patches or -np      Don't apply patches"
	echo "  --force-patches or -fp   Re-apply patches even if apparently not needed"
	echo "  --quiet or -q            Don't print progress"
	exit
}

for i ; do
	case "$i" in
		--help)
			autogen_help
			;;
		--no-recurse|-nr)
			recurse=no
			;;
		--no-patches|-np)
			apply_patches=no
			;;
		--force-patches|-fp)
			apply_patches=yes
			force_patches=yes
			;;
		--quiet|-q)
			quiet=yes
			;;
	esac
done

## When configuring an installable autogen script, these will be replaced
## by config.status.
prefix="@prefix@"
exec_prefix="@exec_prefix@"
sharedstatedir="@sharedstatedir@"
datarootdir="@datarootdir@"
datadir="@datadir@"
toolsdir="@autogen_toolsdir@"

## Check whether this is an installed (or at least, installable) version
## or whether it's being used in-place. If the latter, the substitution
## variables will be untouched.
if test x"$toolsdir" = x"@"autogen_toolsdir"@" ; then
	toolsdir="`dirname \"$appname\"`"
	toolsdir="`(cd \"$toolsdir\" && pwd)`"
fi

fail() {
	echo "$appname: $*" >&2
	exit 1
}

oprogress() {
	test x"$quiet" = x"no" && echo ">>> $*"
}
progress() {
	test x"$quiet" = x"no" && echo " +> $*"
}

if test -r "$srcdir/autogen.rc" ; then
	. "$srcdir/autogen.rc"
fi

if test x"${PATCHES}" = x"" ; then
	true
elif test x"$apply_patches" = x"yes" ; then
	here="`pwd`"
	patchdir="$srcdir/patches"
	oprogress "Applying patches from $patchdir"
	for i in ${PATCHES} ; do
		subdir=`echo $i | cut -f1 -d:`
		patch=`echo $i | cut -f2- -d:`
		if test x"$force_patches" = x"no" && test -r $here/.$patch-applied ; then
			progress "Skipping $patch (already applied)"
		elif test x"$subdir" = x"" ; then
			progress "$patch"
			cd $srcdir && ${GPATCH} -p1 < $patchdir/$patch || exit $?
		elif test -d "$srcdir/$subdir" ; then
			progress "$subdir -> $patch"
			cd $srcdir/$subdir && ${GPATCH} -p1 < $patchdir/$patch || exit $?
		else
			progress "Skipping $patch (subdirectory $subdir is not present)"
			continue
		fi
		touch $here/.$patch-applied
	done
	cd "$here"
fi

if test x"${SUBDIRS}" = x"" ; then
	true
elif test x"$recurse" = x"yes" ; then
	for i in ${SUBDIRS} ; do
		if test -d "$i" ; then
			( cd "$srcdir/$i" && "$toolsdir/autogen" ) || exit $?
		else
			echo "autogen: $srcdir/$i does not exist" >&2
		fi
	done
fi

if test x"${DECL_H_PATH}" = x"" || test x"${DECL_H_PREFIX}" = x"" ; then
	true
else
	sed -e "s!%PREFIX%!${DECL_H_PREFIX}!g" < "$toolsdir/decl.h.in" > "${DECL_H_PATH}.in"
	if test -r "${DECL_H_PATH}" && diff "${DECL_H_PATH}" "${DECL_H_PATH}.in" >/dev/null ; then
		progress "${DECL_H_PATH} is unchanged"
		rm -f "${DECL_H_PATH}.in"
	else
		progress "Installing ${DECL_H_PATH}"
		mv "${DECL_H_PATH}.in" "${DECL_H_PATH}"
	fi
fi

test -r "$srcdir/configure.ac" || fail "Cannot find configure.ac in $srcdir"

if test -f autogen.sh ; then
	oprogress "Running autoreconf to generate files in $srcdir"
	${AUTORECONF} -f -i
	exit
fi
oprogress "Generating files in $srcdir"
progress "Generating aclocal.m4"
aclocalopts="-I$toolsdir -I$toolsdir/ext"
if test -d "$srcdir/m4" ; then
	aclocalopts="$aclocalopts -I$srcdir/m4"
fi
${ACLOCAL110} $aclocalopts || exit

if egrep '^AC_CONFIG_HEADER' "$srcdir/configure.ac" >/dev/null ; then
	progress "Generating config.h.in"
	${AUTOHEADER26} || exit
fi
if egrep '^AC_PROG_LIBTOOL' "$srcdir/configure.ac" >/dev/null ; then
	progress "Generating ltmain.sh"
	${LIBTOOLIZE15} --force --copy --automake || exit
fi

if egrep "^AM_INIT_AUTOMAKE" "$srcdir/configure.ac" >/dev/null ; then
	progress "Generating Makefile.in from Makefile.am"
	${AUTOMAKE110} --add-missing --copy || exit
fi

progress "Generating configure script"
${AUTOCONF26} || exit
